﻿using CustomCharacter;
using CustomCharacter.Ability;
using Exiled.API.Features;
using Exiled.Events.EventArgs.Interfaces;
using Exiled.Events.EventArgs.Player;
using PlayerRoles;
using PlayerStatsSystem;
using RgCore.Events;
using System.Collections.Generic;
using CustomCharacter.Interfaces;
using UnityEngine;

namespace Scp181
{
    [RoleInfo("SCP-181", RoleTypeId.ClassD, RoleTypeId.ClassD)]
    public class Scp181 : CustomCharacter.CustomCharacter, IScpNumber, IRoundStartSpawn, ITrialCard
    {
        public Scp181(int id) : base(id)
        {
        }

        public int Cost => 350;
        public string ScpNumber => "181";
        public override Color32 RoleColor => new Color32(255, 217, 102, 255);

        public byte MinPlayers => 0;
        private static AbilityOutput Passive1_Ability(Player player, ref AbilityInfo info, IExiledEvent e, AbilityTrigger trigger)
        {
            if (e == null || trigger != AbilityTrigger.Event)
            {
                return new AbilityOutput(false);
            }
            if (e is HurtingEventArgs ev && ev.DamageHandler.Base is AttackerDamageHandler && ev.Player != ev.Attacker)
            {
                if (ev.Player == player && (ev.DamageHandler.Damage >= 50f || ev.DamageHandler.Damage < 0f))
                {
                    ev.IsAllowed = false;
                    player.AddAhp(30f, sustain: 30, efficacy: 1f);
                    return new AbilityOutput(true);
                }
            }
            return new AbilityOutput(false);
        }

        public static AbilityOutput Attribute_1(Player player, ref AbilityInfo info, IExiledEvent e, AbilityTrigger trigger)
        {
            if (e == null || trigger != AbilityTrigger.Event)
            {
                return new AbilityOutput(false);
            }
            if (e is UnlockingGeneratorEventArgs ev1 && ev1.Player == player)
            {
                if (UnityEngine.Random.Range(1, 11) >= 9)
                    ev1.IsAllowed = true;
                return new AbilityOutput(true);
            }
            if (e is InteractingDoorEventArgs ev2 && !ev2.Door.IsLocked && ev2.Player == player)
            {
                if (UnityEngine.Random.Range(1, 11) >= 9)
                    ev2.IsAllowed = true;
                return new AbilityOutput(true);
            }
            if (e is InteractingLockerEventArgs ev3 && ev3.Player == player)
            {
                if (UnityEngine.Random.Range(1, 11) >= 9)
                    ev3.IsAllowed = true;
                return new AbilityOutput(true);
            }
            return new AbilityOutput(false);
        }

        public override IEnumerable<Ability> AbilityRegister()
        {
            return new Ability[]
            {
                new Ability("Scp181_Passive_1", "在受到50HP以上的伤害时，会立即无效此攻击，并获得30点100%减伤护盾", AbilityType.Passive,
    10, Passive1_Ability, null, new List<IExiledEvent> { EventProcessor.OnHurting }, null, GetType()),
                new AttributeAbility("Scp181_Attr_1", "对门/发电机/SCP收容柜互动时，有20%几率允许访问。",
                Attribute_1, false, new IExiledEvent[] {
                    Exiled.Events.Handlers.Player.InteractingDoor,
                    Exiled.Events.Handlers.Player.InteractingLocker,
                    Exiled.Events.Handlers.Player.UnlockingGenerator
                }, null, GetType())
            };
        }
    }
}